﻿
// MoyuDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "Moyu.h"
#include "MoyuDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMoyuDlg 对话框
#define TRANSPARENT_COLOR	RGB(255,200,200)
// CTransparenceWindowsDlg 对话框
HINSTANCE hInst = NULL;//DLL指针
typedef BOOL(WINAPI* MYFUNC)(HWND, COLORREF, BYTE, DWORD);
MYFUNC pFun = NULL;//函数指针


CMoyuDlg::CMoyuDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MOYU_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	WndStatus = FALSE;
	CoverArea = NULL;
}

CMoyuDlg::~CMoyuDlg()
{
	if (CoverArea != NULL)
	{
		delete CoverArea;
		CoverArea = NULL;
	}
}

void CMoyuDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCCOLORBUTTON1, m_SelectColor);
	DDX_Control(pDX, IDC_SLIDER1, m_PosY);
	DDX_Control(pDX, IDC_SLIDER3, m_PosX);
	DDX_Control(pDX, IDC_SLIDER2, m_SizeH);
	DDX_Control(pDX, IDC_SLIDER4, m_SizeW);
	DDX_Control(pDX, IDC_SLIDER5, m_Transparence);
}

BEGIN_MESSAGE_MAP(CMoyuDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BtnOpen, &CMoyuDlg::OnBnClickedBtnopen)
	ON_BN_CLICKED(IDC_BtnClose, &CMoyuDlg::OnBnClickedBtnclose)
	ON_BN_CLICKED(IDC_MFCCOLORBUTTON1, &CMoyuDlg::OnBnClickedMfccolorbutton1)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, &CMoyuDlg::OnNMCustomdrawSlider1)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER3, &CMoyuDlg::OnNMCustomdrawSlider3)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, &CMoyuDlg::OnNMCustomdrawSlider2)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER4, &CMoyuDlg::OnNMCustomdrawSlider4)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER5, &CMoyuDlg::OnNMCustomdrawSlider5)
END_MESSAGE_MAP()


// CMoyuDlg 消息处理程序

BOOL CMoyuDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	//得到显示器大小
	ScreenW = GetSystemMetrics(SM_CXSCREEN);
	ScreenH = GetSystemMetrics(SM_CYSCREEN);

	m_PosX.SetRange(1, ScreenW);
	m_PosY.SetRange(1, ScreenH);
	m_SizeW.SetRange(1, ScreenW);
	m_SizeH.SetRange(1, ScreenH);
	m_Transparence.SetRange(0, 255);
	m_SizeW.SetPos(ScreenW / 2);
	m_SizeH.SetPos(ScreenH / 2);
	m_Transparence.SetPos(210);
	m_SelectColor.EnableAutomaticButton(_T("默认值"), RGB(0, 0, 0));
	m_SelectColor.EnableWindow(FALSE);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMoyuDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMoyuDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMoyuDlg::OnBnClickedBtnopen()
{
	// TODO: 在此添加控件通知处理程序代码
	if (WndStatus == TRUE)
	{
		return;
	}
	CoverArea = new COverDlg();
	CoverArea->Create(IDD_TOPDLG,this);
	CoverArea->ShowWindow(SW_SHOW);
	AreaRePaint();
	WndStatus = TRUE;
}


void CMoyuDlg::OnBnClickedBtnclose()
{
	// TODO: 在此添加控件通知处理程序代码
	if (WndStatus == FALSE)
	{
		return;
	}
	if (CoverArea != NULL)
	{
		CoverArea->DestroyWindow();
		delete CoverArea;
		CoverArea = NULL;
	}
	WndStatus = FALSE;
}


void CMoyuDlg::OnBnClickedMfccolorbutton1()
{
	// TODO: 在此添加控件通知处理程序代码
	AreaRePaint();
}

void CMoyuDlg::AreaRePaint()
{
	if (CoverArea == NULL)
	{
		return;
	}
	SetWindowLong(CoverArea->m_hWnd, GWL_EXSTYLE, GetWindowLong(CoverArea->m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);//要使使窗体拥有透明效果,首先要有WS_EX_LAYERED扩展属性
	hInst = LoadLibrary(L"User32.DLL"); //显式加载DLL
	if (hInst != NULL)
	{
		pFun = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");//取得SetLayeredWindowAttributes函数指针
		if (pFun != NULL)
			pFun(CoverArea->m_hWnd, m_SelectColor.GetColor(), m_Transparence.GetPos(), LWA_ALPHA);//整个窗口按透明度透明化			
	}
	int cx = m_PosX.GetPos();
	cx = ScreenW - m_PosX.GetPos();

	m_SizeW.SetRange(1, ScreenW - m_PosX.GetPos());
	m_SizeH.SetRange(1, ScreenH - m_PosY.GetPos());

	//再用MoveWindow
	CRect rcTemp;
	rcTemp.TopLeft() = CPoint(m_PosX.GetPos(), m_PosY.GetPos());
	rcTemp.BottomRight() = CPoint(m_PosX.GetPos() + m_SizeW.GetPos(), m_PosY.GetPos() + m_SizeH.GetPos());
	//MoveWindow(&rcTemp);
	CoverArea->MoveWindow(&rcTemp);
}


void CMoyuDlg::OnNMCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	AreaRePaint();
}


void CMoyuDlg::OnNMCustomdrawSlider3(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	AreaRePaint();
}


void CMoyuDlg::OnNMCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	AreaRePaint();
}


void CMoyuDlg::OnNMCustomdrawSlider4(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	AreaRePaint();
}


void CMoyuDlg::OnNMCustomdrawSlider5(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	AreaRePaint();
}
